package org.boot.fast.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 文件存储配置类
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "fast.file")
public class FastFilePropertiesConfiguration {

    // 文件存储路径
    private String filePath = defaultStoragePath();

    @PostConstruct
    private void init(){
        createDir(this.filePath); // 目录不存在时，自动创建
    }


    private String defaultStoragePath(){
//        if (System.getProperty("os.name").toLowerCase().contains("win")) {
//            return "C:/home/fast/files/"; //windows环境下
//        } else {
//            return "/home/fast/files/"; //linux/mac环境下
//        }
        return "/home/fast/files/";
    }

    private void createDir(String defaultStoragePath) {
        Path path = Paths.get(defaultStoragePath);
        if (Files.notExists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                throw new RuntimeException("自动创建目录失败: " + defaultStoragePath, e);
            }
        }
    }
}